package testng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.FlowEngineApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * @author cheng.huaxing
 * @date 2019-03-12
 */
@SpringBootTest(classes = FlowEngineApplication.class)
public class FlowTaskControllerTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @BeforeClass
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    //@Test
    public void testHandle() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/task/handle").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"taskId\":\"450026\",\n" +
                        "  \"variables\":{\n" +
                        "    \"approval\":\"pass\",\n" +
                        "    \"comment\":\"通过\"\n" +
                        "}\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testHandle2() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/task/handle").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"taskId\":\"4500260\",\n" +
                        "  \"variables\":{\n" +
                        "    \"approval\":\"pass\",\n" +
                        "    \"comment\":\"通过\"\n" +
                        "}\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 1020001);
    }

    //@Test
    public void testList() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/task/todo_list").characterEncoding("UTF-8")
                .content("{}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testClaim() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/task/claim").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"taskId\":\"872834\" "+
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testSkipList() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/task/skip/list").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processInstanceId\":\"660001\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testSkip() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/task/skip").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processInstanceId\":\"660001\",\n" +
                        "  \"currentActivityId\":\"ceshiqianshou\",\n" +
                        "  \"newActivityId\":\"id1468a695\",\n" +
                        "  \"comment\":\"跳转\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testSkip2() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/task/skip").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processInstanceId\":\"6600011\",\n" +
                        "  \"currentActivityId\":\"ceshiqianshou\",\n" +
                        "  \"newActivityId\":\"id1468a695\",\n" +
                        "  \"comment\":\"跳转\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 1020001);
    }
}